package cn.zhangjie.org.controller;

import cn.zhangjie.basic.annotation.PreAuthorize;
import cn.zhangjie.basic.util.JsonResult;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.org.domain.Department;
import cn.zhangjie.org.query.DepartmentQuery;
import cn.zhangjie.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //获取部门树
    @GetMapping("/deptTree")
    @PreAuthorize(name = "部门树",sn = "department:tree") //value后面用来做权限校验
    @ApiOperation(value = "获取部门树-无限极接口")
    public  List<Department> deptTree(){
        return departmentService.deptTree();
    }

    @GetMapping
    @ApiOperation(value = "查询所有" )
    @PreAuthorize(name = "所有部门",sn = "department:getall")
    public List<Department> findall(){
        return departmentService.findAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条" )
    @PreAuthorize(name = "根据id查询部门",sn = "department:get")
    public Department findone(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    @PreAuthorize(name = "删除部门",sn = "department:delete")
    public JsonResult delete(@PathVariable("id") Long id){

        try {
            departmentService.del(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    @PreAuthorize(name = "保存部门",sn = "department:save")
    public JsonResult updataoradd(@RequestBody Department department){

        try {
            if(department.getId()==null){
                departmentService.add(department);
                return JsonResult.me();
            }else {
                departmentService.update(department);
                return JsonResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "分页部门",sn = "department:page")
    public PageList<Department> pagequery(@RequestBody DepartmentQuery departmentQuery){
        try {
            return departmentService.queryPage(departmentQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(name = "删除部门",sn = "department:deletes")
    public JsonResult pactchdel(@RequestBody List<Long> ids){

        try {
            departmentService.pactchdel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("批量删除失败");
        }
    }







}
